package com.asset.dispoal.company.service.commom;

import com.asset.dispoal.company.domain.db.mapper.SysCompanyMapper;
import com.asset.dispoal.company.dto.CompanyLawFirmCourtInsert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.sugar.Attempt;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Component
public class InertLawFirmCourtCommand implements BaseCommand<CompanyLawFirmCourtInsert, Result> {

    @Autowired
    SysCompanyMapper companyMapper;

    public Result execute(CompanyLawFirmCourtInsert companyLawFirmCourtInsert) {
        companyMapper.deleteLawFirmCourt(companyLawFirmCourtInsert.getLawFirmId());
        Attempt.supply(() -> {
            List<String> courtList = Stream.of(StringUtil.split(companyLawFirmCourtInsert.getCourtIdList(), ",")).collect(Collectors.toList());
            for (String courtId : courtList) {
                companyMapper.insertLawFirmCourt(companyLawFirmCourtInsert.getLawFirmId(), courtId);
            }
            return null;
        }, (ex) -> {
            log.debug("清除所有得选择");
            return null;
        }).get();

        return Result.ok();
    }
}
